package com.ywm.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ywm.dto.LoginUser;
import com.ywm.dto.TopUser;
import com.ywm.entity.User;
import com.ywm.service.UserSrevice;
import com.ywm.utils.JwtUtils;
import com.ywm.utils.Response;
import com.ywm.utils.ResponseResult;
import com.ywm.entity.UserVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.util.Assert;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


@RestController
public class UserController {

    @Autowired
    UserSrevice userSrevice;
    @Autowired
    JwtUtils jwtUtils;

    /**登录*/
//    @CrossOrigin
    @PostMapping(value = "/login")
    public ResponseResult<UserVo> login(@RequestBody LoginUser loginUser,HttpServletResponse response){
        //获取用户
        User user = userSrevice.getOne(new QueryWrapper<User>().eq("username", loginUser.getUsername()));
        //判断用户名密码
        Assert.notNull(user,"用户名密码错误！");
        System.out.println("登录"+loginUser.getPassword());
        System.out.println("登录"+SecureUtil.md5(loginUser.getPassword()));
        if(!user.getPassword().equals(SecureUtil.md5(loginUser.getPassword()))){
            return Response.makeErrRsp("用户名密码错误！");
        }
        //生成jwt
        String jwt = jwtUtils.generateToken(user.getUserId());
        //将jwt返回
        response.setHeader("Authorization",jwt);
        response.setHeader("Access-Control-Expose-Headers", "Authorization");
        //将user的信息拷贝到uservo中
        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(user,userVo);
        return Response.makeOKRsp(userVo);
    }
    /**退出*/
    @GetMapping(value = "/logout")
    public ResponseResult<UserVo> logout(){
        SecurityUtils.getSubject().logout();
        return Response.makeOKRsp();
    }
    @PostMapping(value = "/register")
    public ResponseResult<User> register(@RequestBody User user){
        //判断用户是否存在
        User exitUser = userSrevice.getOne(new QueryWrapper<User>().eq("username", user.getUsername()));
        if(exitUser != null){
            return Response.makeErrRsp("用户名已存在");
        }
        //获取密码加密
        String password = user.getPassword();
        user.setPassword(SecureUtil.md5(password));
        boolean save = userSrevice.save(user);
        if(save){
            return Response.makeOKRsp();
        }
        return Response.makeErrRsp("注册失败,请重试");
    }
    /**获取用户信息*/
    @GetMapping(value = "/info/{userId}")
    public ResponseResult<UserVo> getUserInfo(@PathVariable String userId){
        User user = userSrevice.getById(userId);
        UserVo userVo = new UserVo();
        BeanUtil.copyProperties(user,userVo);
        return Response.makeOKRsp(userVo);
    }
    /**修改用户信息*/
    @PostMapping("/user/updateUser")
    public ResponseResult<Object> updateUser(@RequestBody User user){
//        User user = new User();
//        BeanUtils.copyProperties(userVo,user);
//        System.out.println("userVo==>"+userVo);
        System.out.println("user==>"+user);
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",user.getUserId());
        boolean update = userSrevice.update(user,wrapper);
        if (update) {
            return new ResponseResult<>(200,"修改个人信息成功",null);
        }else {
            return new ResponseResult<>(500,"修改个人信息失败",null);
        }
    }

    /**
     * @return 返回发表量前五的作者
     */
    @GetMapping("getTopFiveUser")
    public List<TopUser> getTopFiveUser(){
        return userSrevice.getTopFiveUser();
    }
}
